<!--  -->
<template>
  <div class="">
    <div class="header_row flex align-center flex-wrap">
      <div class="flex align-center m-r-2">
        <span>方案名称：</span>
        <el-input v-model="searchObj.Name" placeholder="搜索方案名称"></el-input>
      </div>

      <div class="flex align-center m-r-2">
        <span>时间：</span>
        <el-date-picker v-model="searchObj.timeValue" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </div>

      <el-button type="primary" @click="handleFilter">查询</el-button>
    </div>

    <div class="m-b-1 neck_box flex align-center space-between">
      <div>
        <el-button type="success" @click="(currentItem = {}), (showForm = true)">新增方案</el-button>
        <!-- <el-button type="danger">批量删除</el-button> -->
        <el-button type="warning" disabled>导出列表</el-button>
      </div>
      <!--<el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="searchObj.currentPage"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="pageTotal"
      >
      </el-pagination>-->
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
    <el-table v-loading="listLoading" :data="list" @sort-change="sortChange" border style="width: 100%" stripe>
      <el-table-column fixed type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="Sname" label="方案名称" align="center">
      </el-table-column>
      <el-table-column prop="Target" label="达标金额" align="center">
      </el-table-column>
      <el-table-column prop="Deduct" label="扣罚金额" align="center">
      </el-table-column>
      <el-table-column prop="Percentage" label="扣罚奖励金额百分比" width="150" align="center">
      </el-table-column>
      <el-table-column prop="StartTime" label="开始时间" align="center" width="180">
      </el-table-column>
      <el-table-column prop="EndTime" label="结束时间" align="center" width="180">
      </el-table-column>
      <el-table-column prop="IsSystem" label="方案类型" align="center">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.IsSystem == 1
                ? "系统方案"
                : scope.row.IsSystem == 2
                ? "身份方案"
                : scope.row.IsSystem == 3
                ? "公司方案"
                : "机构团队方案"
            }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="UserName" label="方案所属" align="center">
      </el-table-column> -->
      <el-table-column prop="Remark" label="备注" align="center">
      </el-table-column>
      <el-table-column prop="UpdateBy" label="修改人员" align="center">
      </el-table-column>
      <el-table-column prop="UpdateDate" label="修改时间" align="center" width="180">
      </el-table-column>
      <el-table-column label="是否有效" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.IsActive" @change="valid(scope.row.Id)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="(currentItem = scope.row), (showForm = true)">编辑</el-button>
          <el-button @click="$delConfirm(() => del(scope.row.Id))" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <PunishForm :show.sync="showForm" :form-data="currentItem" @finish="getList"></PunishForm>
  </div>
</template>

<script>
  import { getPunish, delPunish, toggleValidPunish } from '@/api/system/activate-punish'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  import { deepClone } from '@/utils'
  import PunishForm from './PunishForm'

  export default {
    //import引入的组件需要注入到对象中才能使用
    name: '',
    components: { Pagination, PunishForm },
    data() {
      //这里存放数据
      return {
        dialogVisible: false,
        dialogType: "add",
        addObj: {

        },
        showForm: false,
        currentItem: {},
        list: null,
        total: 0,
        listLoading: true,
        searchObjTmpl: {},
        searchObj: {
          Name: '',
          StartTime: '',
          EndTime: '',
          timeValue: ''
        },
        listQuery: {
          PageIndex: 1,
          PageSize: 10,
          IsPaging: true, //是否分页
          SortName: 'CreateDate',
          SortType: false
        },
      };
    },
    //监听属性 类似于data概念
    computed: {
      dialogTitle() {
        return this.dialogType == 'add' ? '新增方案' : '编辑方案'
      },
    },
    //监控data中的数据变化
    watch: {},
    created() {
      this.searchObjTmpl = deepClone(this.searchObj)
      this.getList()
    },
    activated() { },
    //方法集合
    methods: {
      handleCurrentChange() { },
      handleClick(index, row) {
        this.handleDialogFun('edit', { index, row })
      },
      handleDelete(index, row) {

      },
      handleDialogFun(type, obj) {
        this.dialogType = type
        this.dialogVisible = true
      },
      resetFilter() {
        this.searchObj = deepClone(this.searchObjTmpl)
        this.handleFilter()
      },
      sortChange(data) {
        const { prop, order } = data
        this.listQuery.SortName = prop
        if (order === 'ascending') {
          this.listQuery.SortType = true
        } else {
          this.listQuery.SortType = false
        }
        this.handleFilter()
      },
      handleFilter() {
        this.listQuery.PageIndex = 1
        this.getList()
      },
      getQuery() {
        const params = {
          ...this.searchObj,
          ...this.listQuery,
        }
        if (params.timeValue && params.timeValue.length) {
          params.StartTime = params.timeValue[0]
          params.EndTime = params.timeValue[1]
        }
        return params
      },
      async getList() {
        this.listLoading = true
        const params = this.getQuery()
        const { Data, Total } = await getPunish(params)
        this.listLoading = false
        this.list = Data || []
        this.total = Total || 0
      },
      async del(id) {
        const { code } = await delPunish({
          Id: id
        })
        if (code == 200) {
          this.$notify({
            title: '删除成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      },
      async valid(id) {
        const { code } = await toggleValidPunish({
          Id: id
        })
        if (code == 200) {
          this.$notify({
            title: '切换成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      }
    },

  }
</script>
//@import url(); 引入公共css类
<style  scoped>
</style>
